@charset "UTF-8";
//
// Map functions
//

@function map-set($map, $keys, $value) {
  $maps: ($map);
  $result: null;

  @if type-of(nth($keys, -1)) == 'map' {
    @warn 'The last key you specified is a map; it will be overrided with \'#{$value}\'.';
  }

  @if length($keys) == 1 {
    @return map-merge(
      $map,
      (
        $keys: $value,
      )
    );
  }

  @for $i from 1 through length($keys) - 1 {
    $current-key: nth($keys, $i);
    $current-map: nth($maps, -1);
    $current-get: map-get($current-map, $current-key);
    @if $current-get == null {
      @warn 'Key \'#{$key}\' doesn\'t exist at current level in map.';
    }
    $maps: append($maps, $current-get);
  }

  @for $i from length($maps) through 1 {
    $current-map: nth($maps, $i);
    $current-key: nth($keys, $i);
    $current-val: if($i == length($maps), $value, $result);
    $result: map-merge(
      $current-map,
      (
        $current-key: $current-val,
      )
    );
  }

  @return $result;
}

@function map-deep-get($map, $keys...) {
  @each $key in $keys {
    $map: map-get($map, $key);
  }

  @return $map;
}

@function map-extend($default-map, $merged-maps... /*, $deep */) {
  $last: nth($merged-maps, -1);
  $deep: $last == true;
  $max: if($deep, length($merged-maps) - 1, length($merged-maps));

  @if $max == 0 {
    @return $default-map;
  }

  @for $i from 1 through $max {
    $current: nth($merged-maps, $i);

    @if not $deep {
      $default-map: map-merge($default-map, $current);
    } @else {
      @each $key, $value in $current {
        @if type-of($value) == 'map' and type-of(map-get($default-map, $key)) == 'map' {
          $value: map-extend(map-get($default-map, $key), $value, true);
        }

        $default-map: map-merge(
          $default-map,
          (
            $key: $value,
          )
        );
      }
    }
  }

  @return $default-map;
}

@function map-merge-settings($default-map, $settings-map) {
  @return map-extend($default-map, $settings-map, true);
}
